package com.neusoft.elm.controller;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    CartService cartService=new CartServiceImpl();

    public Object listCart(HttpServletRequest request)throws Exception{
        String businessStr = request.getParameter("businessId");
        Integer businessId=null;
        if(businessStr!=null){
            businessId = Integer.valueOf(businessStr);
        }
        String userId = request.getParameter("userId");
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        List<Cart> list = cartService.listCart(cart);
        return list;
    }

    public Object saveCart(HttpServletRequest request)throws Exception{
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        int i = cartService.saveCart(cart);
        return i;
    }

    public Object removeCart(HttpServletRequest request)throws Exception{
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        int i = cartService.removeCart(cart);
        return i;
    }

    public Object updateCart(HttpServletRequest request)throws Exception{
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        String userId = request.getParameter("userId");
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        cart.setFoodId(foodId);
        cart.setQuantity(quantity);
        int i = cartService.updateCart(cart);
        return i;
    }
}
